package net.thxjj.adminServer.controller;

import net.thxjj.adminServer.controller.base.BaseController;
import net.thxjj.adminServer.entity.*;
import net.thxjj.adminServer.entity.aep.profilegc.ProfileGCInfoBean;
import net.thxjj.adminServer.entity.aep.profilegc.ProfileGCLoginBean;
import net.thxjj.adminServer.service.IDevHistoryService;
import net.thxjj.adminServer.service.IDeviceService;
import net.thxjj.adminServer.service.IProfileGCService;
import net.thxjj.adminServer.service.IUserApiService;
import net.thxjj.adminServer.util.DataUtils;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.adminServer.util.Tools;
import net.thxjj.sitermodel.Device;
import net.thxjj.sitermodel.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/api/profile-gc")
public class ProfileGCController extends BaseController {

    @Autowired
    private IDeviceService iDeviceService;
    @Autowired
    private IProfileGCService iProfileGCService;

    @Autowired
    private IDevHistoryService iDevHistoryService;
    @Autowired
    private IUserApiService iUserApiService;

    /**
     * 获取设备详细数据
     * 包括基础信息和告警标识信息\GAS CO浓度等等
     * @return
     */
    @RequestMapping(value="/detail",method= RequestMethod.GET)
    @ResponseBody
    public Object detail(){

        String deviceId = this.getParameterMap().getString("deviceId");

        if(StringUtils.isEmpty(deviceId)) return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        Device deviceBean = iDeviceService.getDeviceInfo(deviceId);
        if(deviceBean==null){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_EXIST_ERROR,null);
        }

        if(!Const.PROFILE_PRODUCT_ID_GC.equals(deviceBean.getProdKey()) &&!Const.PROFILE_PRODUCT_ID_GC_2.equals(deviceBean.getProdKey()) ){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_SUPPORT_PROTOCOL,null);
        }

        ProfileGCLoginBean profileGCLoginBean = iProfileGCService.ShadowLoginStatus(deviceId);
        ProfileGCInfoBean profileGCInfoBean = iProfileGCService.ShadowStatus(deviceId);
        if(profileGCInfoBean!=null){
            profileGCInfoBean.transToBean();
        }
        ParameterMap heart = iProfileGCService.HeartBeat(deviceId);
        ParameterMap parameterMap1 = new ParameterMap();
        parameterMap1.put("logininfo",profileGCLoginBean);
        parameterMap1.put("info",profileGCInfoBean);
        parameterMap1.put("heartbeat",heart);

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("devBaseInfo",deviceBean);
        parameterMap.put("devDetailInfo",parameterMap1);


        List<ParameterMap> alarmitems = new ArrayList<>();
        ParameterMap parameterMapMultiLogin =  iProfileGCService.isMultiLoginError(deviceId);
        long value = Long.parseLong(parameterMapMultiLogin.getString("value"));
        long timet = Long.parseLong(parameterMapMultiLogin.getString("timestamp"));
        ParameterMap parameterMapMultiLoginReal = new ParameterMap();
        if(value==1){
            parameterMapMultiLoginReal.put("type",2);
            parameterMapMultiLoginReal.put("title","设备多次登录异常");
        }else{
            parameterMapMultiLoginReal.put("type",1);
            parameterMapMultiLoginReal.put("title","设备无登录异常");
        }
        parameterMapMultiLoginReal.put("timestamp",timet);
        alarmitems.add(parameterMapMultiLoginReal);

        if(profileGCInfoBean!=null&&profileGCInfoBean.getProtocol_Number()==1 && profileGCInfoBean.getUpdateBean1()!=null){

            if(profileGCInfoBean.getUpdateBean1().isDismantle()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","拆除报警");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean1().isLifeStop()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","寿命终止");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean1().isLowBattery()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","低压报警");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean1().isSDComAbnormal()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","与底板通信异常");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean1().isLocalTestAlarm()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",3);
                parameter.put("title","本地测试报警");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean1().isSmokeAlarm()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",3);
                parameter.put("title","烟雾报警");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean1().isSensorFault()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","烟雾传感器故障");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

        }else if(profileGCInfoBean!=null&&profileGCInfoBean.getProtocol_Number()==2 && profileGCInfoBean.getUpdateBean2()!=null){

            if(profileGCInfoBean.getUpdateBean2().isLocalTestAlarm()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",3);
                parameter.put("title","本地测试报警");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean2().isRemoteTestAlarm()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",3);
                parameter.put("title","远程测试报警");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean2().isLowBattery()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","低压报警");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean2().isLifeStop()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","寿命终止");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean2().isSDComAbnormal()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","底板通信板通信异常");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean2().isGasAlarm()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",3);
                parameter.put("title","gas报警");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean2().isGasSensorFault()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","gas传感器故障");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean2().isCoAlarm()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",3);
                parameter.put("title","co报警");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

            if(profileGCInfoBean.getUpdateBean2().isCoSensorFault()){
                ParameterMap parameter = new ParameterMap();
                parameter.put("type",2);
                parameter.put("title","co传感器故障");
                parameter.put("timestamp",profileGCInfoBean.getTimestamp());
                alarmitems.add(parameter);
            }

        }else if(profileGCInfoBean!=null&&profileGCInfoBean.getProtocol_Number()==3 && profileGCInfoBean.getUpdateBean3()!=null){

            long status = profileGCInfoBean.getUpdateBean3().getDevice_status();
            ParameterMap parameter = new ParameterMap();
            parameter.put("timestamp",profileGCInfoBean.getTimestamp());
            switch ((int)status){
                case 1:
                    parameter.put("type",3);
                    parameter.put("title","报警");
                    alarmitems.add(parameter);
                    break;
                case 3:
                    parameter.put("type",2);
                    parameter.put("title","传感器故障");
                    alarmitems.add(parameter);
                    break;
                case 6:
                    parameter.put("type",2);
                    parameter.put("title","寿命终止");
                    alarmitems.add(parameter);
                    break;
                case 9:
                    parameter.put("type",3);
                    parameter.put("title","测试报警");
                    alarmitems.add(parameter);
                    break;
                case 10:
                    parameter.put("type",2);
                    parameter.put("title","通讯故障");
                    alarmitems.add(parameter);
                    break;
            }

        }

        parameterMap.put("alarmList",alarmitems);

        return ResponseModel.getModel(ResultEnum.SUCCESS,parameterMap);
    }

    /**
     * 深度数据分析
     * @return
     */
    @RequestMapping(value="/deepAnalysis",method= RequestMethod.GET)
    @ResponseBody
    public Object deepAnalysis(){

        String deviceId = this.getParameterMap().getString("deviceId");

        String start_time = this.getParameterMap().getString("start_time");

        String end_time = this.getParameterMap().getString("end_time");


        if(StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(start_time) || StringUtils.isEmpty(end_time)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        if(!Tools.isLongNumber(start_time) || !Tools.isLongNumber(end_time)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
        }


        Device device = iDeviceService.getDeviceInfo(deviceId);
        if(device==null){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_EXIST_ERROR,null);
        }

        if(!Const.PROFILE_PRODUCT_ID_GC.equals(device.getProdKey()) &&!Const.PROFILE_PRODUCT_ID_GC_2.equals(device.getProdKey()) ){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_SUPPORT_PROTOCOL,null);
        }

        long start_time_long = Long.parseLong(start_time);
        long end_time_long = Long.parseLong(end_time);


        List<List<Object>> listCSQ = new ArrayList<>();
        List<List<Object>> listsnr = new ArrayList<>();
        List<List<Object>> listrsrp = new ArrayList<>();
        List<List<Object>> listrsrq = new ArrayList<>();
        List<List<Object>> listearfcn = new ArrayList<>();
        List<List<Object>> listpci = new ArrayList<>();
        List<List<Object>> listtac = new ArrayList<>();
        List<List<Object>> listecl = new ArrayList<>();

        List<List<Object>> listbattery = new ArrayList<>();
        List<List<Object>> listtemp = new ArrayList<>();
        List<List<Object>> listir = new ArrayList<>();
        List<List<Object>> listr = new ArrayList<>();
        List<List<Object>> listgas = new ArrayList<>();
        List<List<Object>> listco = new ArrayList<>();


        List<ProfileGCInfoBean> list = iProfileGCService.getInfoList(deviceId,start_time_long,end_time_long);

        double maxsbatterylevel=0,minsbatterylevel=0,sumbatterylevel=0,
                maxIR=0,minIR=0,sumIR=0,
                maxR=0,minR=0,sumR=0,
                maxtemp=0,mintemp=0,sumtemp=0,
                maxgasLevel=0,mingasLevel=0,sumgasLevel=0,
                maxcoLevel=0,mincoLevel=0,sumcoLevel=0;
        long countbatterylevel =0,countIR =0,countR =0,counttemp =0,countgasLevel =0,countcoLevel =0;
        boolean flagbatterylevel =false,flagIR =false,flagR =false,flagtemp =false,flaggasLevel =false,flagcoLevel =false;

        for(int i=0;i<list.size();i++){
            ProfileGCInfoBean profileLog = list.get(i);
            List<Object> csq = new ArrayList<>();
            List<Object> snr = new ArrayList<>();
            List<Object> earfcn = new ArrayList<>();
            List<Object> pci = new ArrayList<>();
            List<Object> tac = new ArrayList<>();
            List<Object> ecl = new ArrayList<>();
            List<Object> rsrp = new ArrayList<>();
            List<Object> rsrq = new ArrayList<>();

            csq.add(profileLog.getTimestamp());
            csq.add(profileLog.getCSQ());
            listCSQ.add(csq);

            snr.add(profileLog.getTimestamp());
            snr.add(profileLog.getSNR());
            listsnr.add(snr);

            earfcn.add(profileLog.getTimestamp());
            earfcn.add(profileLog.getEARFCN());
            listearfcn.add(earfcn);

            pci.add(profileLog.getTimestamp());
            pci.add(profileLog.getPCI());
            listpci.add(pci);

            tac.add(profileLog.getTimestamp());
            tac.add(profileLog.getTAC());
            listtac.add(tac);

            ecl.add(profileLog.getTimestamp());
            ecl.add(profileLog.getECL());
            listecl.add(ecl);

            rsrp.add(profileLog.getTimestamp());
            rsrp.add(profileLog.getRSRP());
            listrsrp.add(rsrp);

            rsrq.add(profileLog.getTimestamp());
            rsrq.add(profileLog.getRSRQ());
            listrsrq.add(rsrq);

            profileLog.transToBean();

            if(profileLog.getProtocol_Number()==1 && profileLog.getUpdateBean1()!=null){
                if(!flagbatterylevel){
                    flagbatterylevel = true;
                    maxsbatterylevel = profileLog.getUpdateBean1().getBattery_Level();
                    minsbatterylevel = profileLog.getUpdateBean1().getBattery_Level();
                }else {
                    if(maxsbatterylevel<profileLog.getUpdateBean1().getBattery_Level()){
                        maxsbatterylevel = profileLog.getUpdateBean1().getBattery_Level();
                    }

                    if(minsbatterylevel>profileLog.getUpdateBean1().getBattery_Level()){
                        minsbatterylevel = profileLog.getUpdateBean1().getBattery_Level();
                    }
                }

                List<Object> battery = new ArrayList<>();
                battery.add(profileLog.getTimestamp());
                battery.add(profileLog.getUpdateBean1().getBattery_Level());
                listbattery.add(battery);
                sumbatterylevel+=profileLog.getUpdateBean1().getBattery_Level();

                if(!flagIR){
                    flagIR = true;
                    maxIR = profileLog.getUpdateBean1().getIR_concentration();
                    minIR = profileLog.getUpdateBean1().getIR_concentration();
                }else {
                    if(maxIR<profileLog.getUpdateBean1().getIR_concentration()){
                        maxIR = profileLog.getUpdateBean1().getIR_concentration();
                    }

                    if(minIR>profileLog.getUpdateBean1().getIR_concentration()){
                        minIR = profileLog.getUpdateBean1().getIR_concentration();
                    }
                }
                List<Object> IR = new ArrayList<>();
                IR.add(profileLog.getTimestamp());
                IR.add(profileLog.getUpdateBean1().getIR_concentration());
                listir.add(IR);
                sumIR+=profileLog.getUpdateBean1().getIR_concentration();

                if(!flagR){
                    flagR = true;
                    maxR = profileLog.getUpdateBean1().getR_concentration();
                    minR = profileLog.getUpdateBean1().getR_concentration();
                }else {
                    if(maxR<profileLog.getUpdateBean1().getR_concentration()){
                        maxR = profileLog.getUpdateBean1().getR_concentration();
                    }

                    if(minR>profileLog.getUpdateBean1().getR_concentration()){
                        minR = profileLog.getUpdateBean1().getR_concentration();
                    }
                }
                List<Object> R = new ArrayList<>();
                R.add(profileLog.getTimestamp());
                R.add(profileLog.getUpdateBean1().getR_concentration());
                listr.add(R);
                sumR+=profileLog.getUpdateBean1().getR_concentration();


                if(!flagtemp){
                    flagtemp = true;
                    maxtemp = profileLog.getUpdateBean1().getTemperature();
                    mintemp = profileLog.getUpdateBean1().getTemperature();
                }else {
                    if(maxtemp<profileLog.getUpdateBean1().getTemperature()){
                        maxtemp = profileLog.getUpdateBean1().getTemperature();
                    }

                    if(mintemp>profileLog.getUpdateBean1().getTemperature()){
                        mintemp = profileLog.getUpdateBean1().getTemperature();
                    }
                }
                List<Object> tem = new ArrayList<>();
                tem.add(profileLog.getTimestamp());
                tem.add(profileLog.getUpdateBean1().getTemperature());
                listtemp.add(tem);
                sumtemp+=profileLog.getUpdateBean1().getTemperature();

                countbatterylevel++;
                countIR++;
                countR++;
                counttemp++;
            }else if(profileLog.getProtocol_Number()==2 && profileLog.getUpdateBean2()!=null){

                if(!flagtemp){
                    flagtemp = true;
                    maxtemp = profileLog.getUpdateBean2().getTemperature();
                    mintemp = profileLog.getUpdateBean2().getTemperature();
                }else {
                    if(maxtemp<profileLog.getUpdateBean2().getTemperature()){
                        maxtemp = profileLog.getUpdateBean2().getTemperature();
                    }

                    if(mintemp>profileLog.getUpdateBean2().getTemperature()){
                        mintemp = profileLog.getUpdateBean2().getTemperature();
                    }
                }
                List<Object> tem = new ArrayList<>();
                tem.add(profileLog.getTimestamp());
                tem.add(profileLog.getUpdateBean2().getTemperature());
                listtemp.add(tem);
                sumtemp+=profileLog.getUpdateBean2().getTemperature();

                if(!flaggasLevel){
                    flaggasLevel = true;
                    maxgasLevel = profileLog.getUpdateBean2().getGasLevel();
                    mingasLevel = profileLog.getUpdateBean2().getGasLevel();
                }else {
                    if(maxgasLevel<profileLog.getUpdateBean2().getGasLevel()){
                        maxgasLevel = profileLog.getUpdateBean2().getGasLevel();
                    }

                    if(mingasLevel>profileLog.getUpdateBean2().getGasLevel()){
                        mingasLevel = profileLog.getUpdateBean2().getGasLevel();
                    }
                }
                List<Object> gas = new ArrayList<>();
                gas.add(profileLog.getTimestamp());
                gas.add(profileLog.getUpdateBean2().getGasLevel());
                listgas.add(gas);
                sumgasLevel+=profileLog.getUpdateBean2().getGasLevel();


                if(!flagcoLevel){
                    flagcoLevel = true;
                    maxcoLevel = profileLog.getUpdateBean2().getCoLevel();
                    mincoLevel = profileLog.getUpdateBean2().getCoLevel();
                }else {
                    if(maxcoLevel<profileLog.getUpdateBean2().getCoLevel()){
                        maxcoLevel = profileLog.getUpdateBean2().getCoLevel();
                    }

                    if(mincoLevel>profileLog.getUpdateBean2().getCoLevel()){
                        mincoLevel = profileLog.getUpdateBean2().getCoLevel();
                    }
                }
                List<Object> co = new ArrayList<>();
                co.add(profileLog.getTimestamp());
                co.add(profileLog.getUpdateBean2().getCoLevel());
                listco.add(co);
                sumcoLevel+=profileLog.getUpdateBean2().getCoLevel();

                counttemp++;
                countgasLevel++;
                countcoLevel++;

            }else if(profileLog.getProtocol_Number()==3 && profileLog.getUpdateBean3()!=null){
                if(!flagbatterylevel){
                    flagbatterylevel = true;
                    maxsbatterylevel = profileLog.getUpdateBean3().getBattery_level();
                    minsbatterylevel = profileLog.getUpdateBean3().getBattery_level();
                }else {
                    if(maxsbatterylevel<profileLog.getUpdateBean3().getBattery_level()){
                        maxsbatterylevel = profileLog.getUpdateBean3().getBattery_level();
                    }

                    if(minsbatterylevel>profileLog.getUpdateBean3().getBattery_level()){
                        minsbatterylevel = profileLog.getUpdateBean3().getBattery_level();
                    }
                }
                List<Object> battery = new ArrayList<>();
                battery.add(profileLog.getTimestamp());
                battery.add(profileLog.getUpdateBean3().getBattery_level());
                listbattery.add(battery);
                sumbatterylevel+=profileLog.getUpdateBean3().getBattery_level();


                if(!flagtemp){
                    flagtemp = true;
                    maxtemp = profileLog.getUpdateBean3().getTemperature();
                    mintemp = profileLog.getUpdateBean3().getTemperature();
                }else {
                    if(maxtemp<profileLog.getUpdateBean3().getTemperature()){
                        maxtemp = profileLog.getUpdateBean3().getTemperature();
                    }

                    if(mintemp>profileLog.getUpdateBean3().getTemperature()){
                        mintemp = profileLog.getUpdateBean3().getTemperature();
                    }
                }
                List<Object> tem = new ArrayList<>();
                tem.add(profileLog.getTimestamp());
                tem.add(profileLog.getUpdateBean3().getTemperature());
                listtemp.add(tem);
                sumtemp+=profileLog.getUpdateBean3().getTemperature();

                if(!flaggasLevel){
                    flaggasLevel = true;
                    maxgasLevel = profileLog.getUpdateBean3().getGasLevel();
                    mingasLevel = profileLog.getUpdateBean3().getGasLevel();
                }else {
                    if(maxgasLevel<profileLog.getUpdateBean3().getGasLevel()){
                        maxgasLevel = profileLog.getUpdateBean3().getGasLevel();
                    }

                    if(mingasLevel>profileLog.getUpdateBean3().getGasLevel()){
                        mingasLevel = profileLog.getUpdateBean3().getGasLevel();
                    }
                }
                List<Object> gas = new ArrayList<>();
                gas.add(profileLog.getTimestamp());
                gas.add(profileLog.getUpdateBean3().getGasLevel());
                listgas.add(gas);
                sumgasLevel+=profileLog.getUpdateBean3().getGasLevel();


                if(!flagcoLevel){
                    flagcoLevel = true;
                    maxcoLevel = profileLog.getUpdateBean3().getCoLevel();
                    mincoLevel = profileLog.getUpdateBean3().getCoLevel();
                }else {
                    if(maxcoLevel<profileLog.getUpdateBean3().getCoLevel()){
                        maxcoLevel = profileLog.getUpdateBean3().getCoLevel();
                    }

                    if(mincoLevel>profileLog.getUpdateBean3().getCoLevel()){
                        mincoLevel = profileLog.getUpdateBean3().getCoLevel();
                    }
                }
                List<Object> co = new ArrayList<>();
                co.add(profileLog.getTimestamp());
                co.add(profileLog.getUpdateBean3().getCoLevel());
                listco.add(co);
                sumcoLevel+=profileLog.getUpdateBean3().getCoLevel();

                countbatterylevel++;
                counttemp++;
                countgasLevel++;
                countcoLevel++;
            }
        }

        StaticBean batterybean = new StaticBean();
        if(countbatterylevel>0){
            batterybean.setMax(DataUtils.getDoubleString(maxsbatterylevel));
            batterybean.setMin(DataUtils.getDoubleString(minsbatterylevel));
            batterybean.setAvg(String.format("%.2f", (sumbatterylevel)/countbatterylevel));
        }else{
            batterybean.setMax("").setMin("").setAvg("");
        }
        batterybean.setHistoryList(listbattery);

        StaticBean tempbean = new StaticBean();
        if(counttemp>0){
            tempbean.setMax(DataUtils.getDoubleString(maxtemp));
            tempbean.setMin(DataUtils.getDoubleString(mintemp));
            tempbean.setAvg(String.format("%.2f", (sumtemp)/counttemp));
        }else{
            tempbean.setMax("").setMin("").setAvg("");
        }
        tempbean.setHistoryList(listtemp);

        StaticBean irbean = new StaticBean();
        if(countIR>0){
            irbean.setMax(DataUtils.getDoubleString(maxIR));
            irbean.setMin(DataUtils.getDoubleString(minIR));
            irbean.setAvg(String.format("%.2f", (sumIR)/countIR));
        }else{
            irbean.setMax("").setMin("").setAvg("");
        }
        irbean.setHistoryList(listir);

        StaticBean rbean = new StaticBean();
        if(countR>0){
            rbean.setMax(DataUtils.getDoubleString(maxR));
            rbean.setMin(DataUtils.getDoubleString(minR));
            rbean.setAvg(String.format("%.2f", (sumR)/countR));
        }else{
            rbean.setMax("").setMin("").setAvg("");
        }
        rbean.setHistoryList(listr);

        StaticBean gasbean = new StaticBean();
        if(countgasLevel>0){
            gasbean.setMax(DataUtils.getDoubleString(maxgasLevel));
            gasbean.setMin(DataUtils.getDoubleString(mingasLevel));
            gasbean.setAvg(String.format("%.2f", (sumgasLevel)/countgasLevel));
        }else{
            gasbean.setMax("").setMin("").setAvg("");
        }
        gasbean.setHistoryList(listgas);

        StaticBean cobean = new StaticBean();
        if(countcoLevel>0){
            cobean.setMax(DataUtils.getDoubleString(maxcoLevel));
            cobean.setMin(DataUtils.getDoubleString(mincoLevel));
            cobean.setAvg(String.format("%.2f", (sumcoLevel)/countcoLevel));
        }else{
            cobean.setMax("").setMin("").setAvg("");
        }
        cobean.setHistoryList(listco);



        StaticBean csqstaticBean = iProfileGCService.getCsqStatic(deviceId,start_time_long,end_time_long);
        StaticBean snrstaticBean = iProfileGCService.getSnrStatic(deviceId,start_time_long,end_time_long);
        StaticBean earfcnstaticBean = iProfileGCService.getEarfcnStatic(deviceId,start_time_long,end_time_long);
        StaticBean pcistaticBean = iProfileGCService.getPciStatic(deviceId,start_time_long,end_time_long);
        StaticBean tacstaticBean = iProfileGCService.getTacStatic(deviceId,start_time_long,end_time_long);
        StaticBean eclstaticBean = iProfileGCService.getEclStatic(deviceId,start_time_long,end_time_long);
        StaticBean rsrpstaticBean = iProfileGCService.getRsrpStatic(deviceId,start_time_long,end_time_long);
        StaticBean rsrqstaticBean = iProfileGCService.getRsrqStatic(deviceId,start_time_long,end_time_long);

        ParameterMap parameterMap_status = iProfileGCService.getInfoStatusList(deviceId,start_time_long,end_time_long);

        csqstaticBean.setHistoryList(listCSQ);
        snrstaticBean.setHistoryList(listsnr);
        earfcnstaticBean.setHistoryList(listearfcn);
        pcistaticBean.setHistoryList(listpci);
        tacstaticBean.setHistoryList(listtac);
        eclstaticBean.setHistoryList(listecl);
        rsrpstaticBean.setHistoryList(listrsrp);
        rsrqstaticBean.setHistoryList(listrsrq);


        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("csq",csqstaticBean);
        parameterMap.put("snr",snrstaticBean);
        parameterMap.put("earfcn",earfcnstaticBean);
        parameterMap.put("pci",pcistaticBean);
        parameterMap.put("tac",tacstaticBean);
        parameterMap.put("ecl",eclstaticBean);
        parameterMap.put("rsrp",rsrpstaticBean);
        parameterMap.put("rsrq",rsrqstaticBean);
        parameterMap.put("temp",tempbean);
        parameterMap.put("battery",batterybean);
        parameterMap.put("ir_con",irbean);
        parameterMap.put("r_con",rbean);
        parameterMap.put("gasLevel",gasbean);
        parameterMap.put("coLevel",cobean);
        parameterMap.put("status",parameterMap_status);

        return ResponseModel.getModel(ResultEnum.SUCCESS,parameterMap);
    }



    /**
     * 获取设备登录日志以及处理日志
     * @return
     */
    @RequestMapping(value="/queryDeviceLoginErrorHistory",method= RequestMethod.GET)
    @ResponseBody
    public Object queryDeviceLoginErrorHistory(){

        String device_id = this.getParameterMap().getString("deviceId");

        if(StringUtils.isEmpty(device_id)) return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);

        Device deviceBean = iDeviceService.getDeviceInfo(device_id);

        if(deviceBean==null){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_EXIST_ERROR,null);
        }

        if(!Const.PROFILE_PRODUCT_ID_GC.equals(deviceBean.getProdKey()) &&!Const.PROFILE_PRODUCT_ID_GC_2.equals(deviceBean.getProdKey()) ){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_SUPPORT_PROTOCOL,null);
        }

        ParameterMap parameterMap = new ParameterMap();
        List<List<Object>> lists = iProfileGCService.getLoginErrList(device_id);
        parameterMap.put("error_state",lists);



        return ResponseModel.getModel(ResultEnum.SUCCESS,parameterMap);
    }

    @RequestMapping(value="/deltWithLoginErr",method= RequestMethod.POST)
    @ResponseBody
    public Object deltWithLoginErr(){

        String device_id = this.getParameterMap().getString("deviceId");

        if(StringUtils.isEmpty(device_id)) return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);

        Device deviceBean = iDeviceService.getDeviceInfo(device_id);
        if(deviceBean==null){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_EXIST_ERROR,null);
        }

        if(!Const.PROFILE_PRODUCT_ID_GC.equals(deviceBean.getProdKey()) && !Const.PROFILE_PRODUCT_ID_GC_2.equals(deviceBean.getProdKey())){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_SUPPORT_PROTOCOL,null);
        }

        ParameterMap parameterMap = iProfileGCService.isMultiLoginError(device_id);

        long value = Long.parseLong(parameterMap.getString("value"));
        if(value== 0){
            return ResponseModel.getModel(ResultEnum.IS_NO_LOGIN_ERROR,null);
        }

        String token = this.getRequest().getHeader("token");
        User userinfo = iUserApiService.getUserInfoByToken(token);
        return iDevHistoryService.handleLoginErr(device_id,userinfo.getUser_id());
    }


    /**
     * 获取近30天设备历史数据
     * 包括
     * 1。告警列表
     * 2。模拟量的数据曲线值：电池电量（AC除外）、设备温度、IR、R浓度、gas浓度、co浓度
     * @return
     */
    @RequestMapping(value="/getHistory",method= RequestMethod.GET)
    @ResponseBody
    public Object getHistory(){

        String deviceId = this.getParameterMap().getString("deviceId");

        if(StringUtils.isEmpty(deviceId)) return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);

        Device deviceBean = iDeviceService.getDeviceInfo(deviceId);
        if(deviceBean==null){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_EXIST_ERROR,null);
        }

        if(!Const.PROFILE_PRODUCT_ID_GC.equals(deviceBean.getProdKey()) && !Const.PROFILE_PRODUCT_ID_GC_2.equals(deviceBean.getProdKey())){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_SUPPORT_PROTOCOL,null);
        }


        Calendar now = Calendar.getInstance();
        long curtime = now.getTime().getTime();
        now.add(Calendar.DAY_OF_MONTH, -30);
        long histime = now.getTime().getTime();

        List<List<Object>> listbattery = new ArrayList<>();
        List<List<Object>> listtemp = new ArrayList<>();
        List<List<Object>> listir = new ArrayList<>();
        List<List<Object>> listr = new ArrayList<>();
        List<List<Object>> listgas = new ArrayList<>();
        List<List<Object>> listco = new ArrayList<>();


        List<ProfileGCInfoBean> list = iProfileGCService.getInfoList(deviceId,histime,curtime);
        List<ProfileGCInfoBean> listAlarm = iProfileGCService.getInfoAlarmList(deviceId,histime,curtime);

        for(int i=0;i<list.size();i++){
            ProfileGCInfoBean profileLog = list.get(i);
            profileLog.transToBean();

            if(profileLog.getProtocol_Number()==1 && profileLog.getUpdateBean1()!=null){


                List<Object> battery = new ArrayList<>();
                battery.add(profileLog.getTimestamp());
                battery.add(profileLog.getUpdateBean1().getBattery_Level());
                listbattery.add(battery);

                List<Object> IR = new ArrayList<>();
                IR.add(profileLog.getTimestamp());
                IR.add(profileLog.getUpdateBean1().getIR_concentration());
                listir.add(IR);

                List<Object> R = new ArrayList<>();
                R.add(profileLog.getTimestamp());
                R.add(profileLog.getUpdateBean1().getR_concentration());
                listr.add(R);

                List<Object> tem = new ArrayList<>();
                tem.add(profileLog.getTimestamp());
                tem.add(profileLog.getUpdateBean1().getTemperature());
                listtemp.add(tem);

            }else if(profileLog.getProtocol_Number()==2 && profileLog.getUpdateBean2()!=null){

                List<Object> tem = new ArrayList<>();
                tem.add(profileLog.getTimestamp());
                tem.add(profileLog.getUpdateBean2().getTemperature());
                listtemp.add(tem);

                List<Object> gas = new ArrayList<>();
                gas.add(profileLog.getTimestamp());
                gas.add(profileLog.getUpdateBean2().getGasLevel());
                listgas.add(gas);

                List<Object> co = new ArrayList<>();
                co.add(profileLog.getTimestamp());
                co.add(profileLog.getUpdateBean2().getCoLevel());
                listco.add(co);


            }else if(profileLog.getProtocol_Number()==3 && profileLog.getUpdateBean3()!=null){

                List<Object> battery = new ArrayList<>();
                battery.add(profileLog.getTimestamp());
                battery.add(profileLog.getUpdateBean3().getBattery_level());
                listbattery.add(battery);

                List<Object> tem = new ArrayList<>();
                tem.add(profileLog.getTimestamp());
                tem.add(profileLog.getUpdateBean3().getTemperature());
                listtemp.add(tem);

                List<Object> gas = new ArrayList<>();
                gas.add(profileLog.getTimestamp());
                gas.add(profileLog.getUpdateBean3().getGasLevel());
                listgas.add(gas);

                List<Object> co = new ArrayList<>();
                co.add(profileLog.getTimestamp());
                co.add(profileLog.getUpdateBean3().getCoLevel());
                listco.add(co);

            }
        }



        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("alarms",listAlarm);
        parameterMap.put("temp",listtemp);
        parameterMap.put("battery",listbattery);
        parameterMap.put("ir_con",listir);
        parameterMap.put("r_con",listr);
        parameterMap.put("gasLevel",listgas);
        parameterMap.put("coLevel",listco);



        return ResponseModel.getModel(ResultEnum.SUCCESS,parameterMap);
    }

    /**
     * 按起止时间查询设备状态原始数据
     * @return
     */
    @RequestMapping(value="/queryDeviceJsonData",method= RequestMethod.GET)
    @ResponseBody
    public Object queryDeviceJsonData(){

        String deviceId = this.getParameterMap().getString("deviceId");
        String start_time = this.getParameterMap().getString("start_time");
        String end_time = this.getParameterMap().getString("end_time");

        if(StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(start_time) || StringUtils.isEmpty(end_time)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        if(!Tools.isLongNumber(start_time) || !Tools.isLongNumber(end_time)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
        }

        Device deviceBean = iDeviceService.getDeviceInfo(deviceId);
        if(deviceBean==null){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_EXIST_ERROR,null);
        }

        if(!Const.PROFILE_PRODUCT_ID_GC.equals(deviceBean.getProdKey()) && !Const.PROFILE_PRODUCT_ID_GC_2.equals(deviceBean.getProdKey())){
            return ResponseModel.getModel(ResultEnum.DEVICE_NOT_SUPPORT_PROTOCOL,null);
        }

        long start_time_long = Long.parseLong(start_time);
        long end_time_long = Long.parseLong(end_time);

        List<StatusBean> list = iProfileGCService.getJsonDataAtTime(deviceId,start_time_long,end_time_long);


        return ResponseModel.getModel(ResultEnum.SUCCESS,list);
    }

}
